package com.szsh.aiot.hsm.mapper;

import com.szsh.aiot.hsm.dto.gateway.IPluginInstallRet;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface IPluginInstallRetMapper {

    /**
     * 分页查询
     *
     * @param broadbandNO
     * @param identity
     * @param page
     * @param limit
     * @return
     */
    @Select({
    "<script>",
        "SELECT serno,broadbandNO,gatewayMac,gatewayModel,pluginName,pluginVersion,",
        "identity,installTime,installResult,failReason,firstTime,firstQuery,secondTime,secondQuery,",
        "thirdTime,thirdQuery",
        " from I_PLUGIN_INSTALL_RET",
        "<where> ",
            "<if test ='broadbandNO != null and broadbandNO !=\"\"'> ",
                "and broadbandNO=#{broadbandNO} ",
            "</if>",
            "<if test ='identity != null and identity !=\"0\"'> ",
                "and identity=#{identity} ",
            "</if>",
        "</where>",
        "order by installTime desc limit #{page}, #{limit}",
    "</script>" })
    List<IPluginInstallRet> queryPaginationData(@Param("broadbandNO") String broadbandNO,
                                                @Param("identity") int identity, @Param("page") int page, @Param("limit") int limit);

    /**
     * 订购关系总数查询
     *
     * @param broadbandNO
     * @param identity
     * @return
     */
    @Select({
    "<script>",
        "SELECT count(*) from I_PLUGIN_INSTALL_RET ",
        "<where> ",
            "<if test ='broadbandNO != null and broadbandNO !=\"\"'> ",
                "and broadbandNO=#{broadbandNO} ",
            "</if>",
            "<if test ='identity != null and identity !=\"0\"'> ",
                "and identity=#{identity} ",
            "</if>",
        "</where>",
    "</script>" })
    Integer getCount(@Param("broadbandNO") String broadbandNO, @Param("identity") int identity);

    @Insert({
        "insert into I_PLUGIN_INSTALL_RET(broadbandNO,gatewayMac,gatewayModel,",
        "pluginName,pluginVersion,identity,installTime,installResult,failReason)",
        " values(#{broadbandNO}, #{gatewayMac},#{gatewayModel}, #{pluginName},#{pluginVersion},",
        " #{identity},#{installTime},#{installResult},#{failReason})"
    })
    int insert(IPluginInstallRet ret);

    /**
     * 第一次查询结果更新
     *
     * @param firstQuery
     * @param gatewayMac
     * @return
     */
    @Update({
            "update I_PLUGIN_INSTALL_RET set firstTime=now(),firstQuery=#{firstQuery}",
            " WHERE gatewayMac=#{gatewayMac} and identity=#{identity} order by installTime desc limit 1" })
    int updateFirstQuery(@Param("firstQuery") Integer firstQuery,
            @Param("gatewayMac") String gatewayMac, @Param("identity") Integer identity);

    /**
     * 第二次查询结果更新
     *
     * @param secondQuery
     * @param gatewayMac
     * @return
     */
    @Update({
            "update I_PLUGIN_INSTALL_RET set secondTime=now(),secondQuery=#{secondQuery}" ,
            " WHERE gatewayMac=#{gatewayMac} and identity=#{identity} order by installTime desc limit 1" })
    int updateSecondQuery(@Param("secondQuery") Integer secondQuery,
            @Param("gatewayMac") String gatewayMac, @Param("identity") Integer identity);

    /**
     * 第三次查询结果更新
     *
     * @param thirdQuery
     * @param gatewayMac
     * @return
     */
    @Update({
            "update I_PLUGIN_INSTALL_RET set thirdTime=now(),thirdQuery=#{thirdQuery}",
            " WHERE gatewayMac=#{gatewayMac} and identity=#{identity} order by installTime desc limit 1" })
    int updateThirdQuery(@Param("thirdQuery") Integer thirdQuery,
            @Param("gatewayMac") String gatewayMac, @Param("identity") Integer identity);

    @Select({ "select broadbandNO,pluginVersion,pluginName,installResult from I_PLUGIN_INSTALL_RET ",
            " WHERE gatewayMac=#{gatewayMac} and identity=#{identity} order by installTime desc limit 1" })
    IPluginInstallRet getPluginInstallRet(@Param("gatewayMac") String gatewayMac,
            @Param("identity") int identity);
}
